package com.cn.wanxi.autoconfig.selector;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.function.Predicate;

/**
 * @ClassName SelfAutoConfigurationImportSelector
 * @Description 通过加载**.factories文件获取需要加载的配置类的全限名
 * @Author JiJiang
 * @Date 2022/10/18 19:11
 * @Version 1.0
 */

public class SelfAutoConfigurationImportSelector2 implements ImportSelector {

    /**
     * @MethodName selectImports
     * @Description 返回需要加载到容器配置类文件的类全限名
     * @Param [importingClassMetadata]
     * @Return java.lang.String[]
     **/
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {


        String[] classNames = null;
        try {
            // 获取资源输入流
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("META-INF/spring.factories");
            // 获取文件里的key，value数据
            Properties properties = new Properties();
            properties.load(inputStream);

            // 获取配置文件里的自动配置需要加载的类
            String value = properties.getProperty("com.cn.wx.autoconfig.SelfDefineEnableConfigClassNames");
            // 通过逗号取出所有的类
            classNames = value.split(",");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return classNames;
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return null;
    }
}
